package com.meida.module.bus.provider.service.impl;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.util.TypeUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.security.OpenHelper;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.bus.client.entity.*;
import com.meida.module.bus.provider.api.service.SyncOAService;
import com.meida.module.bus.provider.api.util.ApiConstants;
import com.meida.module.bus.provider.mapper.BusMaterialMapper;
import com.meida.module.bus.provider.service.*;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 项目材料接口实现类
 *
 * @author flyme
 * @date 2020-10-09
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusMaterialServiceImpl extends BaseServiceImpl<BusMaterialMapper, BusMaterial> implements BusMaterialService {

    @Autowired
    private BusMaterialPlan100Service busMaterialPlan100Service;
    @Autowired
    private BusVersionService busVersionService;
    @Autowired
    private BusProjectService busProjectService;
    @Autowired
    private BusMaterialPackage100Service busMaterialPackage100Service;
    @Autowired
    private BusMaterialViewService busMaterialViewService;
    @Resource
    private BusMaterialMapper busMaterialMapper;
    @Autowired
    private BusMajorService busMajorService;
    @Autowired
    private SyncOAService syncOAService;

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusMaterial material, EntityMap extra) {
//        BusProject project = busProjectService.getById(material.getProjectId());
//        EntityMap entityMap = cs.getRequestMap();
//        if (FlymeUtils.isEmpty(entityMap.get("serviceCode")) && FlymeUtils.isEmpty(entityMap.get("materialWbsCode"))) {
//            return ResultBody.failed("缺少参数");
//        }
//        if (FlymeUtils.isNotEmpty(entityMap.get("serviceCode")) && FlymeUtils.isNotEmpty(entityMap.get("materialWbsCode"))) {
//            return ResultBody.failed("服务代码与物料代码不能同时存在");
//        }
//        if (FlymeUtils.isNotEmpty(project)) {
//            Long versionId = IdWorker.getId();
//            Long materialId = IdWorker.getId();
//            BusVersion version = new BusVersion();
//            if (FlymeUtils.isNotEmpty(project.getVersionId())) {
//                BusVersion latestVersion = busVersionService.getLatestVersion(project.getProjectId());
//                BusVersion newBusVersion = new BusVersion();
//                BeanUtils.copyProperties(latestVersion, newBusVersion);
//                String smallVerion = newBusVersion.getSmallVerion();
//                if (FlymeUtils.isNotEmpty(smallVerion)) {
//                    Integer small = TypeUtils.castToInt(smallVerion) + 1;
//                    if (small < 10) {
//                        smallVerion = "0" + small;
//                    } else {
//                        smallVerion = TypeUtils.castToString(small);
//                    }
//                    version.setSmallVerion(smallVerion);
//                }
//                version.setUserId(OpenHelper.getUserId());
//            } else {
//                version.setVersionId(versionId);
//                version.setBigVersion("100");
//                version.setUserId(OpenHelper.getUserId());
//                version.setSmallVerion("00");
//                version.setProjectId(material.getProjectId());
//                version.setState(1);//1正常 0锁定
//            }
//            material.setParentId(0L);
//            material.setBeforeMarkQuantity(entityMap.get("beforeMarkQuantity"));
//            material.setMaterialId(materialId);
//            material.setProjectId(project.getProjectId());
//            material.setConversionUnit(entityMap.get("conversionUnit"));
//            material.setContractBillQuantity(entityMap.getBigDecimal("contractBillQuantity"));
//            material.setContractSyntheticalPrice1(entityMap.getBigDecimal("contractMainPrice1").add(entityMap.getBigDecimal("contractOtherPrice1")));//合计  综合单价
//            material.setContractSyntheticalPrice2(entityMap.getBigDecimal("contractMainPrice2").add(entityMap.getBigDecimal("contractOtherPrice2")));//合计 拟合同综合单价
//            material.setContractSyntheticalPrice3((entityMap.getBigDecimal("contractMainPrice3").add(entityMap.getBigDecimal("contractOtherPrice3"))));//合计 其他综合单价
//            material.setMainAmount(entityMap.getBigDecimal("contractMainPrice1"));//主材价
//            material.setLoss(entityMap.get("loss"));
//            material.setContractBillQuantity(entityMap.getBigDecimal("contractMainPrice1").add(entityMap.getBigDecimal("contractOtherPrice1")));//需要计算
//            material.setConvertBillQuantity100(entityMap.getBigDecimal("contractMainPrice1").add(entityMap.getBigDecimal("contractOtherPrice1")));//需要计算
//            material.setBeforeMarkQuantity(entityMap.getBigDecimal("beforeMarkQuantity"));
//            material.setContractBillQuantity(entityMap.getBigDecimal("contractBillQuantity"));
//            material.setMeasurementUnit(entityMap.get("measurementUnit"));
//            material.setPlanPurchaseCount(entityMap.getBigDecimal("planPurchaseCount"));
//            material.setConvertPurchaseCount(entityMap.getBigDecimal("convertPurchaseCount"));
//            if (FlymeUtils.isNotEmpty(entityMap.get("serviceCode"))) {
//                BusMaterialPackage100 busPackage100 = new BusMaterialPackage100();
//                busPackage100.setMaterialPackage100Id(IdWorker.getId());
//                busPackage100.setProjectId(project.getProjectId());
//                busPackage100.setVersionId(version.getVersionId());
//                busPackage100.setMaterialId(materialId);
//                busPackage100.setMajorName(entityMap.get("majorName"));
//                busPackage100.setMajorCode(entityMap.get("majorCode"));
//                busPackage100.setPackageName(entityMap.get("projectName"));
//                busPackage100.setPackageNo(entityMap.get("projectCode"));
//                busPackage100.setServiceCode(entityMap.get("serviceCode"));
//                busPackage100.setServiceWbsCode(entityMap.get("serviceWbsCode"));
//                busPackage100.setSubjectName(entityMap.get("subjectName"));
//                busPackage100.setPackageDesc(entityMap.get("packageDesc"));
//                busPackage100.setContractDesc(entityMap.get("contractDesc"));
//                busPackage100.setMaterialArea(entityMap.get("materialArea"));
//                busPackage100.setMaterialUse(entityMap.get("materialUse"));
//                busPackage100.setCostQuantity100(entityMap.getBigDecimal("costQuantity100"));
//                busPackage100.setCostQuantity200(entityMap.getBigDecimal("costQuantity200"));
//                busPackage100.setPackageDesc(entityMap.get("packageDesc"));
//                busPackage100.setContractInstallPrice1(entityMap.getBigDecimal("contractInstallPrice1"));
//                busPackage100.setContractInstallPrice2(entityMap.getBigDecimal("contractInstallPrice2"));
//                busPackage100.setContractInstallPrice3(entityMap.getBigDecimal("contractInstallPrice3"));
//                busPackage100.setContractOtherPrice3(entityMap.getBigDecimal("contractOtherPrice3"));
//                busPackage100.setContractOtherPrice1(entityMap.getBigDecimal("contractOtherPrice1"));
//                busPackage100.setContractOtherPrice2(entityMap.getBigDecimal("contractOtherPrice2"));
//                busPackage100.setStatus(0);//初始化导入状态 0 未转100 1转为100
//                boolean a = busMaterialPackage100Service.save(busPackage100);
//            }
//            if (FlymeUtils.isNotEmpty(entityMap.get("materialWbsCode"))) {
//                BusMaterialPlan100 busPlan100 = new BusMaterialPlan100();
//                String levelNo = entityMap.get("levelNo");
//                busPlan100.setLevelNo(levelNo);
//                Map<String, Object> busplanMap = Maps.newHashMap();
//                busplanMap.put("levelNo", levelNo);
//                List<BusMaterialPlan100> plan100List = (List<BusMaterialPlan100>) busMaterialPlan100Service.listByMap(busplanMap);
//                if (FlymeUtils.isNotEmpty(plan100List)) {
//                    material.setParentId(plan100List.get(0).getMaterialId());
//                } else {
//                    material.setParentId(0L);
//                }
//                busPlan100.setMaterialPlanId(IdWorker.getId());
//                busPlan100.setMaterialId(materialId);
//                busPlan100.setProjectId(project.getProjectId());
//                busPlan100.setVersionId(version.getVersionId());
//                busPlan100.setMajorName(entityMap.get("majorName"));
//                busPlan100.setMajorCode(entityMap.get("majorCode"));
//                busPlan100.setMaterialDesc(entityMap.get("materialDesc"));
//                busPlan100.setPackageName(entityMap.get("packageName"));
//                busPlan100.setPackageNo(entityMap.get("packageNo"));
//                busPlan100.setMaterialWbsCode(entityMap.get("materialWbsCode"));
//                busPlan100.setMaterialName(entityMap.get("materialWbsCode"));
//                busPlan100.setMaterialCode(entityMap.get("materialCode"));
//                busPlan100.setSubjectName(entityMap.get("subjectName"));
//                busPlan100.setContractDesc(entityMap.get("contractDesc"));
//                busPlan100.setMaterialArea(entityMap.get("materialArea"));
//                busPlan100.setMaterialUse(entityMap.get("materialUse"));
//                busPlan100.setCostQuantity100(entityMap.getBigDecimal("costQuantity100"));
//                busPlan100.setCostQuantity200(entityMap.getBigDecimal("costQuantity200"));
//                busPlan100.setContractMainPrice1(entityMap.getBigDecimal("contractMainPrice1"));
//                busPlan100.setContractMainPrice2(entityMap.getBigDecimal("contractInstallPrice2"));
//                busPlan100.setContractMainPrice3(entityMap.getBigDecimal("contractInstallPrice3"));
//                busPlan100.setContractOtherPrice1(entityMap.getBigDecimal("contractOtherPrice1"));
//                busPlan100.setContractOtherPrice2(entityMap.getBigDecimal("contractOtherPrice2"));
//                busPlan100.setContractOtherPrice3(entityMap.getBigDecimal("contractOtherPrice3"));
//                busPlan100.setMeasurementUnit(entityMap.get("measurementUnit"));
//                busPlan100.setBrandName(entityMap.get("brandName"));
//                busPlan100.setRemark(entityMap.get("remark"));
//                busPlan100.setLoss(entityMap.get("loss"));
//                busPlan100.setTechnicalData(entityMap.get("technicalData"));
//                busPlan100.setMateriaSpecs(entityMap.get("materiaSpecs"));
//                busPlan100.setStatus(0);//是否转存100 0否 1是
//                busPlan100.setOrderflg("0");//是否开口订单 0否 1是
//                //100材料初始化数据导入
//                boolean b = busMaterialPlan100Service.save(busPlan100);
//            }
//            material.setMaterialId(materialId);
//            project.setVersionId(version.getVersionId());//项目的初始化版本
//            project.setUserId(OpenHelper.getUserId());//导入数据操作人
//            busProjectService.saveOrUpdate(project);
//            busVersionService.save(version);//保存版本信息
//        }
        return ResultBody.ok();
    }


    @Override
    public EntityMap getBusMaterialView(Map map){
        CriteriaQuery<BusMaterial> cq=new CriteriaQuery<BusMaterial>(BusMaterial.class);
        cq.select("bmv.brandName," +
            "	bmv.contractDesc," +
            "	bmv.contractMainPrice1," +
            "	bmv.contractMainPrice2," +
            "	bmv.contractMainPrice3," +
            "	bmv.contractOtherPrice1," +
            "	bmv.contractOtherPrice2," +
            "	bmv.contractOtherPrice3," +
            "   bmv.contractInstallPrice1," +
            "   bmv.contractInstallPrice2," +
            "   bmv.contractInstallPrice3," +
            "   bmv.serviceCode ," +
            "   bmv.serviceWbsCode," +
            "	bmv.costQuantity100," +
            "	bmv.costQuantity200," +
            "	bmv.levelNo," +
            "   bmv.majorName," +
            "   bmv.majorCode," +
            "	bmv.materialArea," +
            "	bmv.materialCode," +
            "	bmv.materialDesc," +
            "	bmv.materialName," +
            "   bmv.packageDesc," +
            "	bmv.materialUse," +
            "	bmv.materialWbsCode," +
            "	bmv.materiaSpecs," +
            "	bmv.packageName," +
            "	bmv.packageNo," +
            "	bmv.remark," +
            "	bmv.subjectName," +
            "	bmv.technicalData," +
            "	bmv.createTime as insertDate,bmv.projectId,bmv.materialPlanId,material.*");
        cq.createJoin(BusMaterialView.class).setMainField("materialId").setJoinField("materialId");
        cq.eq("materialPlanId",map.get("materialPlanId"));
        return selectEntityMap(cq).get(0);
    }



    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusMaterial> cq, BusMaterial material, EntityMap requestMap) {
        Long projectId=Long.parseLong(requestMap.get("projectId").toString());
//        BusVersion planbusVersion=busVersionService.getLatestVersion(projectId,1);
//        BusVersion packagebusVersion=busVersionService.getLatestVersion(projectId,2);
        cq.select(BusMaterial.class, "*");
        cq.addSelect("bmv.brandName," +
            "	bmv.contractDesc," +
            "	bmv.contractMainPrice1," +
            "	bmv.contractMainPrice2," +
            "	bmv.contractMainPrice3," +
            "	bmv.contractOtherPrice1," +
            "	bmv.contractOtherPrice2," +
            "	bmv.contractOtherPrice3," +
            "   bmv.contractInstallPrice1," +
            "   bmv.contractInstallPrice2," +
            "   bmv.contractInstallPrice3," +
            "   bmv.serviceCode ," +
            "   bmv.serviceWbsCode," +
            "	bmv.costQuantity100," +
            "	bmv.costQuantity200," +
            "	bmv.levelNo," +
            "   bmv.majorName," +
            "   bmv.majorCode," +
            "	bmv.materialArea," +
            "	bmv.materialCode," +
            "	bmv.materialDesc," +
            "	bmv.materialName," +
            "   bmv.packageDesc," +
            "	bmv.materialUse," +
            "	bmv.materialWbsCode," +
            "	bmv.materiaSpecs," +
            "	bmv.packageName," +
            "	bmv.packageNo," +
            "	bmv.remark," +
            "	bmv.subjectName," +
            "	bmv.technicalData," +
            "	bmv.createTime as insertDate,bmv.type,bmv.projectId,bmv.materialPlanId");
        cq.createJoin(BusMaterialView.class).setMainField("materialId").setJoinField("materialId");
//        cq.createJoin(BusProject.class).setMainField("projectId").setJoinField("projectId");
//        cq.createJoin(BusVersion.class).setMainField("versionId").setJoinField("versionId");
        cq.like("majorName", requestMap.get("majorName"));
        cq.like("majorCode", requestMap.get("majorCode"));
        cq.like("packageName", requestMap.get("packageName"));
        cq.like("packageNo", requestMap.get("packageNo"));
        cq.like("materialWbsCode", requestMap.get("materialWbsCode"));
        cq.like("materialName", requestMap.get("materialName"));
        cq.like("serviceCode", requestMap.get("serviceCode"));
        cq.like("serviceWbsCode", requestMap.get("serviceWbsCode"));
        cq.like("levelNo", requestMap.get("levelNo"));
        cq.like("subjectName", requestMap.get("subjectName"));
        cq.like("materialArea", requestMap.get("materialArea"));
        cq.eq("material.projectId", requestMap.get("projectId"));
//   cq.isNotNull("parentId");//不包含合并过的版本 转存200时候合并的数据
        if (FlymeUtils.isNotEmpty(requestMap.get("versionId"))) {
            cq.eq("bmv.versionId", requestMap.get("versionId"));
        }
        cq.in("parentId","0","null");
        cq.orderByAsc("bmv.type");
        cq.orderByAsc("bmv.levelNo");
        cq.orderByDesc("material.createTime");
        return ResultBody.ok();
    }

    @Override
    public ResultBody getexcel(String fileName, HttpServletResponse response, Workbook workbook) {
        if (fileName != null) {
            ServletOutputStream out = null;
            try {
                // 以流的形式下载文件   这种方法，打成jar包之后，下载的文件，不会被损坏
                InputStream fis = FileUtil.getInputStream("static/" + fileName + ".xlsx");
                if (fis != null) {
                    //处理文件名中文
                    response.setContentType("application/binary;charset=ISO8859-1");
                    String fileNames = java.net.URLEncoder.encode(fileName, "UTF-8");
                    response.setHeader("Content-disposition", "attachment; filename=" + fileNames + ".xlsx");
                    out = response.getOutputStream();
                    workbook.write(out);
                } else {
                    return ResultBody.failed("文件不存在");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    out.flush();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    @Override
    public ResultBody updateMateialdetail(Map entityMap) {
        Long materialId = Long.parseLong(entityMap.get("id").toString());
        CriteriaQuery<BusMaterial> cq = new CriteriaQuery<BusMaterial>(BusMaterial.class);
        cq.select("bmv.*");
        cq.createJoin(BusMaterialView.class).setJoinField("materialId").setMainField("materialId");
        cq.eq("bmv.materialId", materialId);
        EntityMap busMaterialdetail = findOne(cq);
        if (FlymeUtils.isNotEmpty(busMaterialdetail)) {
            Long planId = busMaterialdetail.getLong("materialPlanId");
            BusMaterialPlan100 busPlan100 = busMaterialPlan100Service.getById(planId);
            BusMaterial busMaterial=getById(materialId);
            if(FlymeUtils.isNotEmpty(busPlan100)) {
                busPlan100.setLevelNo(entityMap.get("levelNo").toString());
                busPlan100.setProjectId(busMaterialdetail.get("projectId"));
                busPlan100.setVersionId(busMaterialdetail.get("versionId"));
                if (FlymeUtils.isNotEmpty(entityMap.get("majorName"))) {
                    busPlan100.setMajorName(entityMap.get("majorName").toString());
                }
                if (FlymeUtils.isNotEmpty(entityMap.get("majorCode"))) {
                    busPlan100.setMajorCode(entityMap.get("majorCode").toString());
                }
                if (FlymeUtils.isNotEmpty(entityMap.get("materialDesc"))) {
                    busPlan100.setMaterialDesc(entityMap.get("materialDesc").toString());
                }
                if (FlymeUtils.isNotEmpty(entityMap.get("packageName"))) {
                    busPlan100.setPackageName(entityMap.get("packageName").toString());
                }
                if (FlymeUtils.isNotEmpty(entityMap.get("packageNo"))) {
                    busPlan100.setPackageNo(entityMap.get("packageNo").toString());
                }
                if (FlymeUtils.isNotEmpty(entityMap.get("materialWbsCode"))) {
                    busPlan100.setMaterialCode(entityMap.get("materialWbsCode").toString());
                }
                if (FlymeUtils.isNotEmpty(entityMap.get("materialCode"))) {
                    busPlan100.setMaterialCode(entityMap.get("materialCode").toString());
                }
                if (FlymeUtils.isNotEmpty(entityMap.get("subjectName"))) {
                    busPlan100.setSubjectName(entityMap.get("subjectName").toString());
                }
                if (FlymeUtils.isNotEmpty(entityMap.get("contractDesc"))) {
                    busPlan100.setContractDesc(entityMap.get("contractDesc").toString());
                }
                if (FlymeUtils.isNotEmpty(entityMap.get("materialArea"))) {
                    busPlan100.setMaterialArea(entityMap.get("materialArea").toString());
                }
                if (FlymeUtils.isNotEmpty(entityMap.get("materialUse"))) {
                    busPlan100.setMaterialUse(entityMap.get("materialUse").toString());
                }
                if (FlymeUtils.isNotEmpty(entityMap.get("beforeMarkQuantity"))) {
                    busMaterial.setBeforeMarkQuantity(TypeUtils.castToBigDecimal(entityMap.get("beforeMarkQuantity")));
                }
                if (FlymeUtils.isNotEmpty(entityMap.get("contractBillQuantity"))) {
                    busMaterial.setContractBillQuantity(TypeUtils.castToBigDecimal(entityMap.get("contractBillQuantity")));
                }

                if (FlymeUtils.isNotEmpty(entityMap.get("costQuantity100")) && FlymeUtils.isNotEmpty(entityMap.get("loss"))) {
                    busPlan100.setCostQuantity100(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100")));
                    busPlan100.setPlanPurchaseCount(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100")).multiply(TypeUtils.castToBigDecimal(entityMap.get("loss"))));
                    busPlan100.setEndPurchaseCount(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100")).multiply(TypeUtils.castToBigDecimal(entityMap.get("loss"))));
                    busMaterial.setMainAmount(busMaterial.getMainPrice().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                    busMaterial.setMainAmount1(busMaterial.getMainPrice1().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                    busMaterial.setMainAmount2(busMaterial.getMainPrice2().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                    busMaterial.setMainAmount3(busMaterial.getMainPrice2().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                    busMaterial.setContractsumSyntheticalPrice1(busMaterial.getMainPrice1().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                    busMaterial.setContractsumSyntheticalPrice2(busMaterial.getMainPrice2().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                    busMaterial.setContractsumSyntheticalPrice3(busMaterial.getMainPrice3().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                    busMaterial.setLoss(TypeUtils.castToBigDecimal(entityMap.get("loss")));
                    busMaterial.setPlanPurchaseCount(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100")).multiply(TypeUtils.castToBigDecimal(entityMap.get("loss"))));
                    busMaterial.setConvertPurchaseCount(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100")).multiply(TypeUtils.castToBigDecimal(entityMap.get("loss"))));
                    busMaterial.setMainsumPrice1(busMaterial.getMainPrice1().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                    busMaterial.setMainsumPrice2(busMaterial.getMainPrice2().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                    busMaterial.setMainsumPrice3(busMaterial.getMainPrice3().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                } else if (FlymeUtils.isNotEmpty(entityMap.get("costQuantity100"))) {
                    busPlan100.setCostQuantity100(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100")));
                    busPlan100.setPlanPurchaseCount(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100")).multiply(busPlan100.getLoss()));
                    busPlan100.setEndPurchaseCount(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100")).multiply(busPlan100.getLoss()));
                    busMaterial.setMainAmount(busMaterial.getMainPrice().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                    busMaterial.setMainAmount1(busMaterial.getMainPrice1().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                    busMaterial.setMainAmount2(busMaterial.getMainPrice2().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                    busMaterial.setMainAmount3(busMaterial.getMainPrice2().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                    busMaterial.setContractsumSyntheticalPrice1(busMaterial.getMainPrice1().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                    busMaterial.setContractsumSyntheticalPrice2(busMaterial.getMainPrice2().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                    busMaterial.setContractsumSyntheticalPrice3(busMaterial.getMainPrice3().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                    busMaterial.setPlanPurchaseCount(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100")).multiply(busPlan100.getLoss()));
                    busMaterial.setConvertPurchaseCount(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100")).multiply(busPlan100.getLoss()));
                    busMaterial.setMainsumPrice1(busMaterial.getMainPrice1().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                    busMaterial.setMainsumPrice2(busMaterial.getMainPrice2().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                    busMaterial.setMainsumPrice3(busMaterial.getMainPrice3().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                } else if (FlymeUtils.isNotEmpty(entityMap.get("loss"))) {
                    busPlan100.setLoss(TypeUtils.castToBigDecimal(entityMap.get("loss")));
                    busPlan100.setPlanPurchaseCount(busPlan100.getCostQuantity100().multiply(TypeUtils.castToBigDecimal(entityMap.get("loss"))));
                    busPlan100.setEndPurchaseCount(busPlan100.getCostQuantity100().multiply(TypeUtils.castToBigDecimal(entityMap.get("loss"))));
                    busMaterial.setMainAmount(busMaterial.getMainPrice().multiply(busPlan100.getCostQuantity100()));
                    busMaterial.setMainAmount1(busMaterial.getMainPrice1().multiply(busPlan100.getCostQuantity100()));
                    busMaterial.setMainAmount2(busMaterial.getMainPrice2().multiply(busPlan100.getCostQuantity100()));
                    busMaterial.setMainAmount3(busMaterial.getMainPrice2().multiply(busPlan100.getCostQuantity100()));
                    busMaterial.setContractsumSyntheticalPrice1(busMaterial.getMainPrice1().multiply(busPlan100.getCostQuantity100()));
                    busMaterial.setContractsumSyntheticalPrice2(busMaterial.getMainPrice2().multiply(busPlan100.getCostQuantity100()));
                    busMaterial.setContractsumSyntheticalPrice3(busMaterial.getMainPrice3().multiply(busPlan100.getCostQuantity100()));
                    busMaterial.setPlanPurchaseCount(busPlan100.getCostQuantity100().multiply(TypeUtils.castToBigDecimal(entityMap.get("loss"))));
                    busMaterial.setConvertPurchaseCount(busPlan100.getCostQuantity100().multiply(TypeUtils.castToBigDecimal(entityMap.get("loss"))));
                    busMaterial.setMainsumPrice1(busMaterial.getMainPrice1().multiply(busPlan100.getCostQuantity100()));
                    busMaterial.setMainsumPrice2(busMaterial.getMainPrice2().multiply(busPlan100.getCostQuantity100()));
                    busMaterial.setMainsumPrice3(busMaterial.getMainPrice3().multiply(busPlan100.getCostQuantity100()));
                }
                if (FlymeUtils.isNotEmpty(entityMap.get("costQuantity200"))) {
                    busPlan100.setCostQuantity200(TypeUtils.castToBigDecimal(entityMap.get("costQuantity200")));
                }
                if (FlymeUtils.isNotEmpty(entityMap.get("measurementUnit"))) {
                    busPlan100.setMeasurementUnit(entityMap.get("measurementUnit").toString());
                }
                if (FlymeUtils.isNotEmpty(entityMap.get("conversionUnit"))) {
                    busMaterial.setConversionUnit(entityMap.get("conversionUnit").toString());
                }
                if (FlymeUtils.isNotEmpty(entityMap.get("materialName"))) {
                    busPlan100.setMaterialName(entityMap.get("materialName").toString());
                }
                if (FlymeUtils.isNotEmpty(entityMap.get("brandName"))) {
                    busPlan100.setBrandName(entityMap.get("brandName").toString());
                }
                if (FlymeUtils.isNotEmpty(entityMap.get("remark"))) {
                    busPlan100.setRemark(entityMap.get("remark").toString());
                }
                if (FlymeUtils.isNotEmpty(entityMap.get("loss"))) {
                    busPlan100.setLoss(BigDecimal.valueOf(Double.parseDouble(entityMap.get("loss").toString())));
                }
                if (FlymeUtils.isNotEmpty(entityMap.get("technicalData"))) {
                    busPlan100.setTechnicalData(entityMap.get("technicalData").toString());
                }
                if (FlymeUtils.isNotEmpty(entityMap.get("materiaSpecs"))) {
                    busPlan100.setMateriaSpecs(entityMap.get("materiaSpecs").toString());
                }
                if (FlymeUtils.isNotEmpty(entityMap.get("mainPrice"))) {
                    busMaterial.setMainAmount(TypeUtils.castToBigDecimal(entityMap.get("mainPrice")).multiply(busPlan100.getCostQuantity100()));
                    busMaterial.setSyntheticalPrice(TypeUtils.castToBigDecimal(entityMap.get("mainPrice")));
                }
                if (FlymeUtils.isNotEmpty(entityMap.get("mainPrice1"))) {
                    busMaterial.setMainAmount1(TypeUtils.castToBigDecimal(entityMap.get("mainPrice1")).multiply(busPlan100.getCostQuantity100()));
                    busMaterial.setContractsumSyntheticalPrice1(TypeUtils.castToBigDecimal(entityMap.get("mainPrice1")).multiply(busPlan100.getCostQuantity100()));
                    busMaterial.setContractSyntheticalPrice1(TypeUtils.castToBigDecimal(entityMap.get("mainPrice1")));
                    busMaterial.setMainsumPrice1(TypeUtils.castToBigDecimal(entityMap.get("mainPrice1")).multiply(busPlan100.getCostQuantity100()));
                    busMaterial.setSyntheticalAmount1(TypeUtils.castToBigDecimal(entityMap.get("mainPrice1")).multiply(busPlan100.getCostQuantity100()));
                    busMaterial.setSyntheticalPrice1(TypeUtils.castToBigDecimal(entityMap.get("mainPrice1")));
                    busMaterial.setMainPrice1(TypeUtils.castToBigDecimal(entityMap.get("mainPrice1")));
                    if (FlymeUtils.isNotEmpty(entityMap.get("mainPrice2"))) {
                        busMaterial.setMainAmount2(TypeUtils.castToBigDecimal(entityMap.get("mainPrice2")).multiply(busPlan100.getCostQuantity100()));
                        busMaterial.setContractsumSyntheticalPrice2(TypeUtils.castToBigDecimal(entityMap.get("mainPrice2")).multiply(busPlan100.getCostQuantity100()));
                        busMaterial.setContractSyntheticalPrice2(TypeUtils.castToBigDecimal(entityMap.get("mainPrice2")));
                        busMaterial.setMainsumPrice2(TypeUtils.castToBigDecimal(entityMap.get("mainPrice2")).multiply(busPlan100.getCostQuantity100()));
                        busMaterial.setSyntheticalAmount2(TypeUtils.castToBigDecimal(entityMap.get("mainPrice2")).multiply(busPlan100.getCostQuantity100()));
                        busMaterial.setSyntheticalPrice2(TypeUtils.castToBigDecimal(entityMap.get("mainPrice2")));
                        busMaterial.setMainPrice2(TypeUtils.castToBigDecimal(entityMap.get("mainPrice2")));
                    }
                    saveOrUpdate(busMaterial);
                    busMaterialPlan100Service.saveOrUpdate(busPlan100);
                }
            }else{
                BusMaterialPackage100 busPackage100 = busMaterialPackage100Service.getById(planId);
                if (FlymeUtils.isNotEmpty(busPackage100)) {
                    busPackage100.setProjectId(busMaterialdetail.get("projectId"));
                    busPackage100.setVersionId(busMaterialdetail.get("versionId"));
                    if (FlymeUtils.isNotEmpty(entityMap.get("majorCode"))) {
                        busPackage100.setMajorCode(entityMap.get("majorCode").toString());
                    }
                    if (FlymeUtils.isNotEmpty(entityMap.get("packageName"))) {
                        busPackage100.setPackageName(entityMap.get("packageName").toString());
                    }
                    if (FlymeUtils.isNotEmpty(entityMap.get("packageNo"))) {
                        busPackage100.setPackageNo(entityMap.get("packageNo").toString());
                    }
                    if (FlymeUtils.isNotEmpty(entityMap.get("beforeMarkQuantity"))) {
                        busMaterial.setBeforeMarkQuantity(TypeUtils.castToBigDecimal(entityMap.get("beforeMarkQuantity")));
                    }
                    if (FlymeUtils.isNotEmpty(entityMap.get("contractBillQuantity"))) {
                        busMaterial.setContractBillQuantity(TypeUtils.castToBigDecimal(entityMap.get("contractBillQuantity")));
                    }
                    if (FlymeUtils.isNotEmpty(entityMap.get("serviceCode"))) {
                        busPackage100.setServiceCode(entityMap.get("serviceCode").toString());
                    }
                    if (FlymeUtils.isNotEmpty(entityMap.get("serviceWbsCode"))) {
                        busPackage100.setServiceWbsCode(entityMap.get("serviceWbsCode").toString());
                    }
                    if (FlymeUtils.isNotEmpty(entityMap.get("subjectName"))) {
                        busPackage100.setSubjectName(entityMap.get("subjectName").toString());
                    }
                    if (FlymeUtils.isNotEmpty(entityMap.get("packageDesc"))) {
                        busPackage100.setPackageDesc(entityMap.get("packageDesc").toString());
                    }
                    if (FlymeUtils.isNotEmpty(entityMap.get("contractDesc"))) {
                        busPackage100.setContractDesc(entityMap.get("contractDesc").toString());
                    }
                    if (FlymeUtils.isNotEmpty(entityMap.get("materialArea"))) {
                        busPackage100.setMaterialArea(entityMap.get("materialArea").toString());
                    }
                    if (FlymeUtils.isNotEmpty(entityMap.get("materialUse"))) {
                        busPackage100.setMaterialUse(entityMap.get("materialUse").toString());
                    }
                    if (FlymeUtils.isNotEmpty(entityMap.get("conversionUnit"))) {
                        busMaterial.setConversionUnit(entityMap.get("conversionUnit").toString());
                    }

                    if (FlymeUtils.isNotEmpty(entityMap.get("costQuantity100"))) {
                        busPackage100.setCostQuantity100(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100")));
                        busMaterial.setSyntheticalAmount(busMaterial.getInstallPrice().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                        busMaterial.setSyntheticalAmount1(busMaterial.getInstallPrice1().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                        busMaterial.setSyntheticalAmount2(busMaterial.getInstallPrice2().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                        busMaterial.setInstallsumPrice1(busMaterial.getInstallPrice1().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                        busMaterial.setInstallsumPrice2(busMaterial.getInstallPrice2().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                        busMaterial.setInstallAmount(busMaterial.getInstallPrice().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                        busMaterial.setInstallAmount1(busMaterial.getInstallPrice1().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                        busMaterial.setInstallAmount2(busMaterial.getInstallPrice2().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                        busMaterial.setContractsumSyntheticalPrice1(busMaterial.getInstallPrice1().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                        busMaterial.setContractsumSyntheticalPrice2(busMaterial.getInstallPrice2().multiply(TypeUtils.castToBigDecimal(entityMap.get("costQuantity100"))));
                    }
                    if (FlymeUtils.isNotEmpty(entityMap.get("costQuantity200"))) {
                        busPackage100.setCostQuantity200(TypeUtils.castToBigDecimal(entityMap.get("costQuantity200")));
                    }
                    if (FlymeUtils.isNotEmpty(entityMap.get("packageDesc"))) {
                        busPackage100.setPackageDesc(entityMap.get("packageDesc").toString());
                    }
                    if (FlymeUtils.isNotEmpty(entityMap.get("installPrice"))) {
                        busMaterial.setInstallPrice(TypeUtils.castToBigDecimal(entityMap.get("installPrice")));
                        busMaterial.setSyntheticalPrice(TypeUtils.castToBigDecimal(entityMap.get("installPrice")));
                        busMaterial.setSyntheticalAmount(TypeUtils.castToBigDecimal(entityMap.get("installPrice")).multiply(busPackage100.getCostQuantity100()));
                        busMaterial.setInstallAmount(TypeUtils.castToBigDecimal(entityMap.get("installPrice")).multiply(busPackage100.getCostQuantity100()));
                    }
                    if (FlymeUtils.isNotEmpty(entityMap.get("installPrice1"))) {
                        busMaterial.setInstallPrice1(TypeUtils.castToBigDecimal(entityMap.get("installPrice1")));
                        busMaterial.setSyntheticalPrice1(TypeUtils.castToBigDecimal(entityMap.get("installPrice1")));
                        busMaterial.setSyntheticalAmount1(TypeUtils.castToBigDecimal(entityMap.get("installPrice1")).multiply(busPackage100.getCostQuantity100()));
                        busMaterial.setInstallsumPrice1(TypeUtils.castToBigDecimal(entityMap.get("installPrice1")).multiply(busPackage100.getCostQuantity100()));
                        busMaterial.setInstallAmount1(TypeUtils.castToBigDecimal(entityMap.get("installPrice1")).multiply(busPackage100.getCostQuantity100()));
                        busMaterial.setContractsumSyntheticalPrice1(TypeUtils.castToBigDecimal(entityMap.get("installPrice1")).multiply(busPackage100.getCostQuantity100()));
                        busMaterial.setContractSyntheticalPrice1(TypeUtils.castToBigDecimal(entityMap.get("installPrice1")).add(busPackage100.getCostQuantity100()));//合计  综合单价
                    }
                    if (FlymeUtils.isNotEmpty(entityMap.get("installPrice2"))) {
                        busMaterial.setInstallPrice2(TypeUtils.castToBigDecimal(entityMap.get("installPrice2")));
                        busMaterial.setSyntheticalPrice2(TypeUtils.castToBigDecimal(entityMap.get("installPrice2")));
                        busMaterial.setSyntheticalAmount2(TypeUtils.castToBigDecimal(entityMap.get("installPrice2")).multiply(busPackage100.getCostQuantity100()));
                        busMaterial.setInstallsumPrice2(TypeUtils.castToBigDecimal(entityMap.get("installPrice2")).multiply(busPackage100.getCostQuantity100()));
                        busMaterial.setInstallAmount2(TypeUtils.castToBigDecimal(entityMap.get("installPrice2")).multiply(busPackage100.getCostQuantity100()));
                        busMaterial.setContractsumSyntheticalPrice2(TypeUtils.castToBigDecimal(entityMap.get("installPrice2")).multiply(busPackage100.getCostQuantity100()));
                        busMaterial.setContractSyntheticalPrice2(TypeUtils.castToBigDecimal(entityMap.get("installPrice2")).add(busPackage100.getCostQuantity100()));//合计  综合单价
                    }
                    saveOrUpdate(busMaterial);
                    busMaterialPackage100Service.saveOrUpdate(busPackage100);
                }
              }

            }

        return ResultBody.ok();
    }

    @Override
    public ResultBody deleteMaterial(Long ids) {
        CriteriaQuery<BusMaterialView> cq = new CriteriaQuery<BusMaterialView>(BusMaterialView.class);
        cq.eq("materialPlanId", ids);
        EntityMap busMaterialView = busMaterialViewService.findOne(cq);
        if (FlymeUtils.isNotEmpty(busMaterialView)) {
            BusMaterialPlan100 plan100 = busMaterialPlan100Service.getById(ids);
            if (FlymeUtils.isNotEmpty(plan100)) {
                boolean a = removeById(plan100.getMaterialId());
                if (a) {
                    busMaterialPlan100Service.removeById(ids);
                }
            }
            BusMaterialPackage100 Package100 = busMaterialPackage100Service.getById(ids);
            if (FlymeUtils.isNotEmpty(Package100)) {
                boolean a = removeById(Package100.getMaterialId());
                if (a) {
                    busMaterialPackage100Service.removeById(ids);
                }
            }
        }
        return ResultBody.ok();
    }

    /**
     * 项目转存100
     * @param materialPlanIds
     * @param projectId
     * @return
     */
    @Override
    public ResultBody updataStatus(Long[] materialPlanIds,Long projectId) {
        BusProject project = busProjectService.getById(projectId);
        List<BusMaterialPlan100> Plan100List = new ArrayList<>();
        List<BusMaterialPackage100> Package100List = new ArrayList<>();
        List<Long> longids = busMaterialPlan100Service.getinitPlanIds(projectId);
        List<Long> longpakgeId = busMaterialPackage100Service.getinitPlanIds(projectId);
        for (Long materialPlanId : longids) {
            BusMaterialPlan100 busMaterialPlan100 = busMaterialPlan100Service.getById(materialPlanId);
            if (FlymeUtils.isNotEmpty(busMaterialPlan100)) {
                busMaterialPlan100.setStatus(1);//转为100
                Plan100List.add(busMaterialPlan100);
            }
        }
        for (Long materialpakgeId : longpakgeId) {
            BusMaterialPackage100 package100 = busMaterialPackage100Service.getById(materialpakgeId);
            if (FlymeUtils.isNotEmpty(package100)) {
                package100.setStatus(1);//转为100
                Package100List.add(package100);
            }
        }
        List<Object> objectList = new ArrayList<>();
        objectList.add(Plan100List);
        objectList.add(Package100List);
       int state= syncOAService.approval(objectList, "/approvl/materials_list", OpenHelper.getUserName(), ApiConstants.BUSINESS_APPROVAL_100, projectId.toString());
       if(state==1){
           project.setState(1);
           busProjectService.saveOrUpdate(project);//更新项目为锁定状态
           return ResultBody.ok("数据已请求，等待是审批");
       }
        return ResultBody.ok();
    }

    @Override
    public ResultBody goArchive100(Long projectId, Integer checkState) {
        if (checkState == ApiConstants.APPROVAL_REJECT) {//审核驳回
            ResultBody resultBody = busProjectService.updateProjectStus(projectId, 0);
            resultBody.setMsg("审批驳回,项目状态已更为正常");
            return resultBody;
        }
        //审批通过
        if (checkState == ApiConstants.APPROVAL_SUCCESS) {//审核通过
            BusProject project = busProjectService.getById(projectId);
            List<BusMaterialPlan100> Plan100List = new ArrayList<>();
            List<BusMaterialPackage100> Package100List = new ArrayList<>();
            List<Long> longids = busMaterialPlan100Service.getinitPlanIds(projectId);
            List<Long> longpakgeId = busMaterialPackage100Service.getinitPlanIds(projectId);
            for (Long materialPlanId : longids) {
                BusMaterialPlan100 busMaterialPlan100 = busMaterialPlan100Service.getById(materialPlanId);
                if (FlymeUtils.isNotEmpty(busMaterialPlan100)) {
                    busMaterialPlan100.setStatus(1);//转为100
                    busMaterialPlan100.setApplayStatus(0);//初转审核状态置为 --
                    Plan100List.add(busMaterialPlan100);
                }
            }
            for (Long materialpakgeId : longpakgeId) {
                BusMaterialPackage100 package100 = busMaterialPackage100Service.getById(materialpakgeId);
                if (FlymeUtils.isNotEmpty(package100)) {
                    package100.setStatus(1);//转为100
                    Package100List.add(package100);
                }
            }
            boolean bpn = false;
            boolean bpe = false;
            if (FlymeUtils.isNotEmpty(Plan100List)) {
                bpn = busMaterialPlan100Service.saveOrUpdateBatch(Plan100List);
            }
            if (FlymeUtils.isNotEmpty(Package100List)) {
                bpe = busMaterialPackage100Service.saveOrUpdateBatch(Package100List);
            }
            if (bpn || bpe) {
                project.setVersion100(1);
                busProjectService.saveOrUpdate(project);
                return ResultBody.ok("数据转存100成功");
            } else {
                return ResultBody.ok("数据转存100失败");
            }
        }
        return ResultBody.ok();
    }

    @Override
    public ResultBody pageMaterList(Map map) {
        BusVersion planbusVersion=busVersionService.getLatestVersion(Long.parseLong(map.get("projectId").toString()),1);
        BusVersion packagebusVersion=busVersionService.getLatestVersion(Long.parseLong(map.get("projectId").toString()),2);
        Long userId = OpenHelper.getUserId();
        List<String> userMajorList = busMajorService.getUserMajors(userId);
        if (userMajorList.size() > 0) {
            if (!userMajorList.contains("P99")) {
                map.put("majorCodes",userMajorList);
            }
        }
        if(FlymeUtils.isNotEmpty(planbusVersion)){
            map.put("bigVersion1",planbusVersion.getBigVersion());
        }else{
            map.put("bigVersion1","");
        }
        if(FlymeUtils.isNotEmpty(packagebusVersion)){
            map.put("bigVersion2",packagebusVersion.getBigVersion());
        }else{
            map.put("bigVersion2","");
        }
        Long  page=Long.parseLong(map.get("page").toString());
        map.put("page",(page-1)*Long.parseLong(map.get("limit").toString()));
        List<EntityMap> resultMap= busMaterialMapper.materialDetailpage(map);
        IPage<EntityMap> page_1=new Page<>(page,Long.parseLong(map.get("limit").toString()),busMaterialMapper.findDetailpageCtn(map));
        page_1.setRecords(resultMap);
        return ResultBody.ok(page_1);
    }

    @Override
    public List<EntityMap> exceportData(Map map) {
        CriteriaQuery<BusMaterial> cq=new CriteriaQuery(BusMaterial.class);
        Long projectId=Long.parseLong(map.get("projectId").toString());
        cq.select(BusMaterial.class, "*");
        cq.addSelect("bmv.brandName," +
            "	bmv.contractDesc," +
            "	bmv.contractMainPrice1," +
            "	bmv.contractMainPrice2," +
            "	bmv.contractMainPrice3," +
            "	bmv.contractOtherPrice1," +
            "	bmv.contractOtherPrice2," +
            "	bmv.contractOtherPrice3," +
            "   bmv.contractInstallPrice1," +
            "   bmv.contractInstallPrice2," +
            "   bmv.contractInstallPrice3," +
            "   bmv.serviceCode ," +
            "   bmv.serviceWbsCode," +
            "	bmv.costQuantity100," +
            "	bmv.costQuantity200," +
            "	bmv.levelNo," +
            "   bmv.majorName," +
            "   bmv.majorCode," +
            "	bmv.materialArea," +
            "	bmv.materialCode," +
            "	bmv.materialDesc," +
            "	bmv.materialName," +
            "   bmv.packageDesc," +
            "	bmv.materialUse," +
            "	bmv.materialWbsCode," +
            "	bmv.materiaSpecs," +
            "	bmv.packageName," +
            "	bmv.packageNo," +
            "	bmv.remark," +
            "	bmv.subjectName," +
            "	bmv.technicalData," +
            "	bmv.createTime as insertDate,bmv.type,bmv.projectId,bmv.materialPlanId");
        cq.createJoin(BusMaterialView.class).setMainField("materialId").setJoinField("materialId");
        cq.createJoin(BusProject.class).setMainField("projectId").setJoinField("projectId");
        cq.like("majorName", map.get("majorName"));
        cq.like("majorCode", map.get("majorCode"));
        cq.like("packageName", map.get("packageName"));
        cq.like("packageNo", map.get("packageNo"));
        cq.like("materialWbsCode", map.get("materialWbsCode"));
        cq.like("materialName", map.get("materialName"));
        cq.like("serviceCode", map.get("serviceCode"));
        cq.like("serviceWbsCode", map.get("serviceWbsCode"));
        cq.like("levelNo", map.get("levelNo"));
        cq.like("subjectName", map.get("subjectName"));
        cq.like("materialArea", map.get("materialArea"));
        cq.eq("bmv.projectId", map.get("projectId"));
        if (FlymeUtils.isNotEmpty(map.get("versionId"))) {
            cq.eq("bmv.versionId", map.get("versionId"));
        }
        cq.eq("material.parentId","0");
        cq.orderByAsc("bmv.levelNo");
        cq.orderByDesc("bmv.createTime");
        return selectEntityMap(cq);
    }

    @Override
    public ResultBody deleteallMaterial(Long projectId) {
        CriteriaQuery<BusMaterialView> cq = new CriteriaQuery<BusMaterialView>(BusMaterialView.class);
        cq.eq("projectId", projectId);
        List<EntityMap> busMaterialView = busMaterialViewService.selectEntityMap(cq);
        if (FlymeUtils.isNotEmpty(busMaterialView)) {
            List<Long> planids=new ArrayList<>();
            List<Long> packageIds=new ArrayList<>();
            List<Long> materialIds=new ArrayList<>();
            for (EntityMap  entityMap:busMaterialView) {
                BusMaterialPlan100 plan100 = busMaterialPlan100Service.getById(entityMap.getLong("materialPlanId"));
                if(FlymeUtils.isNotEmpty(plan100)){
                    planids.add(plan100.getMaterialPlanId());
                }
                BusMaterialPackage100 Package100 = busMaterialPackage100Service.getById(entityMap.getLong("materialPlanId"));
                if(FlymeUtils.isNotEmpty(Package100)){
                    packageIds.add(Package100.getMaterialPackage100Id());
                }
                materialIds.add(entityMap.getLong("materialId"));
            }
            busMaterialPlan100Service.removeByIds(planids);
            busMaterialPackage100Service.removeByIds(packageIds);
            removeByIds(materialIds);
        }
        return ResultBody.ok();
    }
}
